package com.ruoyi.tws.service.impl;

import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.domain.TwsUseRecord;
import com.ruoyi.tws.service.IOrderService;
import com.ruoyi.tws.service.ITwsUseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.web.controller.BaseServiceImpl;
import com.ruoyi.tws.mapper.OrderMonetaryChangeMapper;
import com.ruoyi.tws.domain.OrderMonetaryChange;
import com.ruoyi.tws.service.IOrderMonetaryChangeService;

import java.util.Date;

/**
 * 运费变更申请Service业务层处理
 *
 * @author wjx
 * @date 2023-05-10
 */
@Service
public class OrderMonetaryChangeServiceImpl extends BaseServiceImpl<OrderMonetaryChangeMapper, OrderMonetaryChange> implements IOrderMonetaryChangeService {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private ITwsUseRecordService useRecordService;

    @Override
    public boolean MonetaryChange(Order order) {
        OrderMonetaryChange monetaryChange = baseMapper.selectById(order.getChangeId());
        monetaryChange.setStatus(1);
        updateById(monetaryChange);

        //操作日志
        TwsUseRecord useRecord = new TwsUseRecord();
        useRecord.setOrderId(order.getId());
        useRecord.setOperateContent("原运费:"+monetaryChange.getTotalMonetaryAmount()+",变更后:"+monetaryChange.getDesireMonetaryAmount());
        useRecord.setOperateTime(new Date());
        useRecord.setOperator(SecurityUtils.getUsername());
        useRecordService.save(useRecord);

        //将订单再次转为待发车
        order.setStatus("1");
        //将订单运费金额变为新金额
        order.setTotalMonetaryAmount(monetaryChange.getDesireMonetaryAmount());
        orderService.updateById(order);

        return true;
    }

    @Override
    public boolean rejectChange(OrderMonetaryChange orderMonetaryChange) {
        Order order = new Order();
        order.setId(orderMonetaryChange.getOrderId());
        order.setStatus("1");
        orderService.updateById(order);

        TwsUseRecord useRecord = new TwsUseRecord();
        useRecord.setOrderId(orderMonetaryChange.getOrderId());
        useRecord.setOperateContent("驳回变更运费申请");
        useRecord.setOperateTime(new Date());
        useRecord.setOperator(SecurityUtils.getUsername());
        useRecordService.save(useRecord);

        orderMonetaryChange.setStatus(2);
        updateById(orderMonetaryChange);
        return true;
    }
}
